package com.dudu.controller;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.dudu.bean.SmsIpBean;
import com.dudu.bean.UserBean;
import com.dudu.enumeration.OperateTypeEnum;
import com.dudu.service.ILogService;
import com.dudu.service.ISmsIpService;
import com.dudu.service.impl.LogService;
import com.dudu.service.impl.SmsIpServiceImpl;
import com.dudu.utils.ValidateUtil;

import core.CoreService;
/**
 * 白名单管理
 * @author gaoxs
 *
 */
public class SmsIpController extends CoreService implements ISmsIpController {
	// 当前登陆用户
	private final String KEY_CURRENT_USER = "key_current_user";
	// 用于记录相应接口调用的日志
	private ILogService logService = new LogService();
	private ISmsIpService smsServcie=new SmsIpServiceImpl();
	/**
	 * 查询白名单，按分页条件查询
	 * @return
	 */
	public String queryAll() {
		log.info("SmsIpController.queryAll");
		JSONObject object = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //获取当前登录用户信息
        UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
        if (currentUser != null) {
        	//获取页面分页用信息
	        String pageSql = this.getPageSql();
	        //获取查询参数信息
	        String paramSql = this.convertParams();
	        // 总数量
	        int totalAcount = this.smsServcie.getTotalAcount( paramSql);
	        List<JSONObject> list = this.smsServcie.queryAll(paramSql, pageSql);
	        jsonArray = JSONArray.fromObject(list);
	        
	        JSONObject dataobject = new JSONObject();
	        object.put("flag", "000000");
	        object.put("desc", "success");
	        dataobject.put("totalItemNum", totalAcount);
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
        } else {
			object.put("flag", "100000");
		}
        return returnData(object.toString());
	}
	
	
	/**
	 * 新增白名单
	 * @return
	 */
	public String doInsert() {
		log.info("SmsIpController.doInsert");
		//最后返回的对象
		JSONObject object = new JSONObject();
		//获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			//获取前台传入的data数据
			String dataStr = (String) this.getRequest().getParameter("data");
			//转化为json对象
			JSONObject data = JSONObject.fromObject(dataStr);
			SmsIpBean bean=new SmsIpBean();
			String signcode=data.getString("signcode");
			bean.setSigncode(signcode);
			String ip=data.getString("ip");
			if(!ValidateUtil.isIpv4(ip)){
				object.put("flag", "100000");
				return returnData(object.toString());
			}
			bean.setIp(ip);
			bean.setComments(data.getString("comments"));
			int flag=smsServcie.doinsert(bean);
			if(flag!=-1){
				object.put("flag", "000000");
			    object.put("desc", "success");
			}else{
				object.put("flag", "100000");
			}
			// 日志输出信息
			logService.log(OperateTypeEnum.ADD.getKey(), currentUser.getId(), currentUser.getName(), 
					"t_sms_ip","签名标识:"+signcode+"增加白名单:"+ip);
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}

	/**
	 * 修改白名单
	 * @return
	 */
	public String doEdit() {
		log.info("SmsIpController.doEdit");
		//最后返回的对象
		JSONObject object = new JSONObject();
		//获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			//获取前台传入的data数据
			String dataStr = (String) this.getRequest().getParameter("data");
			//转化为json对象
			JSONObject data = JSONObject.fromObject(dataStr);
			Integer id=data.getInt("id");
			if(id!=null){
				SmsIpBean bean=smsServcie.getBeanById(id);
				String signcode=data.getString("signcode");
				bean.setSigncode(signcode);
				String ip=data.getString("ip");
				if(!ValidateUtil.isIpv4(ip)){
					object.put("flag", "100000");
					return returnData(object.toString());
				}
				bean.setIp(ip);
				bean.setComments(data.getString("comments"));
				int flag=smsServcie.doEdit(bean);
				if(flag!=-1){
					object.put("flag", "000000");
				    object.put("desc", "success");
				}else{
					object.put("flag", "100000");
				}
				// 日志输出信息
				logService.log(OperateTypeEnum.EDIT.getKey(), currentUser.getId(), currentUser.getName(), 
						"t_sms_ip","签名标识:"+signcode+"修改成白名单:"+ip);
			}else{
				object.put("flag", "100000");
			}
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}

	/**
	 * DEMO
	 * @return
	 */
	public String demo() {
		log.info("SmsIpController.doInsert");
		//最后返回的对象
		JSONObject object = new JSONObject();
		//获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}

	

}
